//
//  SeekEquipmentViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/2/27.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "SeekEquipmentViewController.h"
#import "ScanViewController.h"
#import "EquipmentViewController.h"
#import "SeekEquipmentTable.h"
#import "SeekEquipmentMoreTable.h"
#import "SeekEquipmentTable.h"
#import "HistoryViewController.h"
@interface SeekEquipmentViewController ()<UITextFieldDelegate>
@property (nonatomic,assign) NSInteger addNum;
@property(nonatomic, strong)UIView *addView;
@property(nonatomic,strong)UITextField *searchField;
@property(nonatomic,strong)NSString *state;
@property(nonatomic)NSInteger click;
@property(nonatomic,strong)UILabel *typeLab;
@property(nonatomic,strong)SeekEquipmentMoreTable *seekMoreTable;
@property(nonatomic,strong)SeekEquipmentTable *seekTable;

@end

@implementation SeekEquipmentViewController
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self.seekMoreTable];
    [[[UIApplication sharedApplication] keyWindow] addSubview:self.addView];


}
- (void)viewWillDisappear:(BOOL)animated
{
    [self.seekMoreTable removeFromSuperview];
    [self.addView removeFromSuperview];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
      [self setTitle:@"查找设备"];
    [self.navBar createRightBtnWithImage:[UIImage imageNamed:@"Ico_detail_add"] block:^{
        _addNum++;
        self.seekMoreTable.hidden=YES;
        if (_addNum%2==0) {
            _addView.hidden=YES;


        }else
        {
            _addView.hidden=NO;
            
        }
    }];
    
    UIImage *addBg=[UIImage imageNamed:@"Ico_progressAddBG"];//@"Ico_home_scan",@"扫一扫",
    NSArray*nameAr=[NSArray arrayWithObjects:@"扫码",@"新增", nil];
    CGFloat imgWidth=70;
    
    _addView=[[UIView alloc]initWithFrame:CGRectMake(WindowWidth-imgWidth-15, 64+3,imgWidth, addBg.size.height+40*nameAr.count+5)];
    _addView.hidden=YES;
    _addView.backgroundColor=[UIColor clearColor];
//    [self.view addSubview:_addView];
//    [self.view bringSubviewToFront:_addView];
    UIImageView*addImgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, imgWidth, 6)];
    addImgView.image=addBg;
    [_addView addSubview:addImgView];
    UIView *imgBgView=[[UIView alloc]initWithFrame:CGRectMake(0, addBg.size.height,imgWidth, 40*nameAr.count)];
    imgBgView.backgroundColor=MainColor;
    imgBgView.layer.cornerRadius=5;
    imgBgView.layer.masksToBounds=YES;
    [_addView addSubview:imgBgView];
    for (int i=0; i<nameAr.count; i++) {
        UIView *bgView=[[UIView alloc]initWithFrame:CGRectMake(0, 40*i, imgWidth, 40)];
        bgView.tag=1000+i;
        [imgBgView addSubview:bgView];
        UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,imgWidth, 40)];
        lab.text=[NSString stringWithFormat:@"%@",[nameAr objectAtIndex:i]];
        [bgView addSubview:lab];
        lab.userInteractionEnabled=YES;
        lab.textColor=[UIColor whiteColor];
        lab.font=FF(15);
        lab.textAlignment=NSTextAlignmentCenter;
        UILabel *line=[[UILabel alloc]initWithFrame:CGRectMake(15, 40-0.5, imgBgView.bounds.size.width-15-7, 0.5)];
        line.backgroundColor=C(@"fd7d60");
        [bgView  addSubview:line];
        if (i==0){
#pragma mark 扫码
            
            [bgView bk_whenTapped:^{
                _addView.hidden=YES;
                //扫码
                ScanViewController *scanVC =[[ScanViewController alloc]init];
                [scanVC setDataType:HasListData];
                scanVC.orderUuid=self.orderUuid;
                scanVC.instUuid=self.instUuid;
                scanVC.num=self.num;
                [self pushController:scanVC Lever:1 Animated:YES];
                
                
                
            }];
        }else if (i==1){
#pragma mark 新增
            
            [bgView bk_whenTapped:^{
                _addView.hidden=YES;
                EquipmentViewController *equipVc=[[EquipmentViewController alloc]init];
                equipVc.orderUuid=self.orderUuid;
                equipVc.instUuid=self.instUuid;
                equipVc.num=self.num;
                //有单
                [self pushController:equipVc Lever:1 Animated:YES];
                
                
                
            }];
        }
    }
    
    self.state=@"";
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 64, WindowWidth, 54)];
    headView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:headView];
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, 0.5)];
    line.backgroundColor=LineColor;
    [headView addSubview:line];
    UIView *line1=[[UIView alloc]initWithFrame:CGRectMake(0, 44-0.5, WindowWidth,0.5)];
    line1.backgroundColor=LineColor;
    [headView addSubview:line1];
    UIView *selectView=[[UIView alloc]initWithFrame:CGRectMake(0.5, 0.5, 90, 43)];
    [headView addSubview:selectView];
    UIImage *moreImg = [UIImage imageNamed:@"Ico_register_down"];
    
    _typeLab=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, selectView.bounds.size.width-moreImg.size.width-15, selectView.bounds.size.height)];
    _typeLab.textColor=C(@"b4b4b4");
    _typeLab.font=FF(14);
    _typeLab.numberOfLines=0;
    _typeLab.text=@"全部";
    _typeLab.textAlignment=NSTextAlignmentRight;
    [selectView addSubview:_typeLab];
    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_typeLab.frame)+5, (selectView.bounds.size.height-moreImg.size.height)/2, moreImg.size.width, moreImg.size.height)];
    imgView.image=moreImg;
    [selectView addSubview:imgView];
    [selectView bk_whenTapped:^{
        self.addView.hidden=YES;
        self.click++;
        if (self.click%2==0) {
            _seekMoreTable.hidden=YES;
            
        }else
        {
            _seekMoreTable.hidden=NO;
            
        }
        
    }];
    UIView *centerView=[[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(selectView.frame), 8, 1, headView.bounds.size.height-16)];
    centerView.backgroundColor=LineColor;
    [headView addSubview:centerView];
    
    _searchField = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(centerView.frame)+8, 0.5, headView.bounds.size.width-CGRectGetMaxX(centerView.frame)-15-8, 43)];
    _searchField.delegate = self;
    _searchField.placeholder = @"搜索";
    _searchField.font=FF(14);
    _searchField.returnKeyType=UIReturnKeyDone;
    //        _searchField.clearButtonMode=UITextFieldViewModeAlways;
    [headView addSubview:_searchField];
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, headView.bounds.size.height -10, WindowWidth,10)];
    lineView.backgroundColor=BackColor;
    [headView addSubview:lineView];
    UIView *line3=[[UIView alloc]initWithFrame:CGRectMake(0, lineView.bounds.size.height -0.5, WindowWidth,0.5)];
    line3.backgroundColor=LineColor;
    [lineView addSubview:line3];
    _seekMoreTable=[[SeekEquipmentMoreTable alloc]initWithFrame:CGRectMake(0, 44+64, WindowWidth, WindowHeight)];
    _seekMoreTable.hidden=YES;
    __block typeof (self)blockSelf=self;
    
    [_seekMoreTable setCCellSelect:^(NSMutableDictionary *dic) {
        blockSelf.click++;
        blockSelf.seekMoreTable.hidden=YES;
        blockSelf.state= [dic objectForKey:@"state"];
        blockSelf.typeLab.text =[dic objectForKey:@"desc"];
        
        NSMutableDictionary *proDic=[[NSMutableDictionary alloc]init];
        [proDic setObject:blockSelf.searchField.text forKey:@"name"];

        if ([blockSelf.state isEqualToString:@""]) {
            
        }else
        {
            [proDic setObject:blockSelf.state forKey:@"bussines"];
            
        }
        [proDic setObject:blockSelf.instUuid forKey:@"inst_uuid"];
        [blockSelf.seekTable relodTableWithDictionary:proDic];
    }];

     _seekTable=[[SeekEquipmentTable alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(headView.frame), WindowWidth, WindowHeight-CGRectGetMaxY(headView.frame))withInstUuid:self.instUuid];
    [self.view addSubview:_seekTable];
    [_seekTable setCCellSelectBack:^(NSDictionary *dic) {
        HistoryViewController *historyVC =[[HistoryViewController alloc]init];
        historyVC.machineUuid =[dic objectForKey:@"uuid"];
        historyVC.orderUuid=blockSelf.orderUuid;
        historyVC.num=blockSelf.num;
        historyVC.Type=MachineUnLink;//需要关联
        [blockSelf pushController:historyVC Lever:1 Animated:YES];
    }];


}
#pragma mark - UITextFieldDelegate
//开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    
    // 搜索关联词汇
    // 获得需要进行搜索的关键字
    NSString *shouldBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    if ([self.state isEqualToString:@""]) {
        
    }else
    {
        [dic setObject:self.state forKey:@"bussines"];
        
    }
    [dic setObject:shouldBeString forKey:@"name"];
    [dic setObject:self.instUuid forKey:@"inst_uuid"];

    [self.seekTable relodTableWithDictionary:dic];
    
    return YES;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:textField.text forKey:@"name"];
    if ([self.state isEqualToString:@""]) {
        
    }else
    {
        [dic setObject:self.state forKey:@"bussines"];
        
    }
    [dic setObject:self.instUuid forKey:@"inst_uuid"];

    [self.seekTable relodTableWithDictionary:dic];
    [textField resignFirstResponder];
    
    
    return YES;
}
//clear
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]init];
    [dic setObject:textField.text forKey:@"name"];
    if ([self.state isEqualToString:@""]) {
        
    }else
    {
        [dic setObject:self.state forKey:@"bussines"];
        
    }
    [dic setObject:self.instUuid forKey:@"inst_uuid"];

    [self.seekTable relodTableWithDictionary:dic];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchField resignFirstResponder];
    [self.view endEditing:YES];
}




@end
